#ifndef DRYCTRL_THREAD_H
#define DRYCTRL_THREAD_H
#include <QThread>
#include <QDateTime>
#include "KgGlobalData.h"
#include "ioreader.h"

//ZZ-IO404 或 ZZ-IO606 线程
class CBattStack;
class DryCtrlThread : public QThread
{
	Q_OBJECT
    friend void updateDryDataToRtdb(DryCtrlThread *pThread);
public:
	DryCtrlThread(QObject *parent);
	~DryCtrlThread();
public:
	void run();
	int readDevDIState(CBattStack *pStack, quint16 &connFailedCount); // 读取DI1--DI6的状态并判断消防告警                         // 读取DO1--DO6的状态
	void setSomeDOState();
    bool setDoState(quint16 doIndex, quint16 usSetVal);//控制DO口是否闭合(打开继电器)

    quint16 waitFzOpt(quint16 ctrlType, quint16 &drvFzCount, quint16 &drvFzSuccedCount, QDateTime &drvFzTime, QDateTime statChangeTime);
	quint16 waitHzOpt(quint16 ctrlType, quint16 &drvHzCount, QDateTime &drvHzTime);
	void parseStackState(quint16 stackState, quint16 &usHasProt, quint16 &usHasAlarm,
		quint16 &usChargeFull, quint16 &usDischargeEmpty, quint16 &usProhibitChgDchg);
	quint16 getConfSwitchState(CBattStack *pStack, DI_STU diStu);// 	获取汇流开关反馈状态 1-合闸 0-分闸
	quint16 getClosedConfSwitchCount();
	QString getConfSwitchStrState();
	bool controlHzFz();
	bool sendHzFzCmd(quint16  usDrvHz, quint16  usDrvFz); // added 2022-11-17
	void detectConfSwitchError(quint16 usDrvHz, quint16 usDrvFz);// added 2023-11-19
	void controlOtherFz();
	void dealwithConnFailed(int runCount);
	void readDIValue(quint16 runCount);

	quint16 getConfSwitchState();// 	获取汇流开关反馈状态 1-合闸 0-分闸
	quint16 getIsolSwitchAggrState();
	quint16 hasEmertStopAlarm();// added 2022-1-25
	quint16 hasFireAlarm();
	
private:
	void judgeXfAlarm();        //  9
	void judgeXfSparyAlam(); //  25
	void judgeXfFaultAlam();  //  26
	void judgeXfAutoManu();  //  27
	void judgeXfMotionMsg(); // 14
	void judgeXfWarmMsg();  // 22
	void judgeElecPowerOff();//  30
	void judgeWaterAlarm();
	void judgeThunderAlarm();
	void judgeEmergeStop();
	void judgeFuseAlarm();
	void judgetConfSwitchState(CBattStack *pStack);
	void judgeIsolSwitchAggrState();
	void judgeMainSwitchState();
	void lszhParseDLQState(quint16 &usHasProt);
	void judgeChgJcqState();
	void judgeCombusGasAlarm();
	bool hasCombusGasAlarm();
	void judgeUpsAlarm();// added 2022-11-8
	void judgeDoorAlarm();
	void judgetResState();
	void judgeAcFault();
	void judgeLqFault();

	void judgeDoorAlarmSec(); // added 2023-12-21
	void judgeWaterAlarmSec();
	void judgeEmergeStopSec();
	void judgeThunderAlarmSec();
	void judgeBmsLocalState();// added 2023-12-25
	void judgeBmsRemoteState();
	
	void sendLog(int iLogType, const QString& strLog);
private:
	IOReader   *m_pIOReader;

	quint16 m_fireCount;
	quint16 m_fireSignal;     // 1:消防信号 0:无 (只记录日志不通知BA）
	quint16 m_fireAlarm;      // 1:消防告警 0:正常
	//quint16 m_fireDetectAlarm;// added 2022-5-25
	quint16 m_oldFireAlarm;

	quint16 m_fireSprayCount;      // added 2022-12-10
	quint16 m_fireSpraySignal;     // 1:消防信号 0:无 (只记录日志不通知BA）
	quint16 m_fireSprayAlarm;      // 1:消防告警 0:正常
	quint16 m_oldFireSprayAlarm;

	quint16 m_fireFaultCount;      // added 2022-12-21
	quint16 m_fireFaultAlarm;      // 1:消防故障 0:正常

	quint16 m_fireManuCount;      // added 2023-11-9
	quint16 m_fireManuMode;       // 1:消防手动 0:消防自动

	quint16 m_elecPowerOffCount;   // added 2023-11-9
	quint16 m_elecPowerOffAlarm;   // 1:市电断电 0:正常

	quint16 m_waterCount;
	quint16 m_waterSignal;     // 1:水浸信号 0:无 (只记录日志不通知BA）
	quint16 m_waterAlarm;      // 1:水浸告警 0:正常

	quint16 m_thunderCount;
	quint16 m_thunderSignal;  // 1:防雷信号 0:无 (只记录日志不通知BA）
	quint16 m_thunderAlarm;   // 1:防雷告警 0:正常

	quint16 m_fuseCount;
	quint16 m_fuseSignal;    // 1:熔断器信号 0:无 (只记录日志不通知BA） // added
	quint16 m_fuseAlarm;     // 1:熔断器告警 0:正常

	quint16 m_xfMotionCount;
	quint16 m_xfMotionAlarm;  // 1:消防释放 0:无  // added 2022-4-6

	quint16 m_xfWarmCount;
	quint16 m_xfWarmMsg;     // 1:消防预警 0:无  // added 2022-6-6

	quint16 m_upsAlarmCount;
	quint16 m_upsAlarm;      // 1:UPS告警  0:无  // added 2022-11-8

	quint16 m_doorAlarmCount;
	quint16 m_doorAlarm;     // 1:门禁告警  0:无  // added 2022-11-8

	quint16 m_doorAlarmCountSec;
	quint16 m_doorAlarmSec;     // 1:门禁告警  0:无  // added 2023-5-4

	quint16 m_emergStopCount;
	quint16 m_emergStopSignal; // 1:急停信号 0:无 (只记录日志不通知BA）
	quint16 m_emergStopAlarm;  // 1:急停  0:正常
	quint16 m_usOldEmerStopState; // added 2022-1-12

	quint16 m_waterCountSec;
	quint16 m_waterSignalSec;     // 1:水浸信号 0:无 (只记录日志不通知BA）
	quint16 m_waterAlarmSec;      // 1:水浸告警 0:正常

	quint16 m_thunderCountSec;
	quint16 m_thunderSignalSec;  // 1:防雷信号 0:无 (只记录日志不通知BA）
	quint16 m_thunderAlarmSec;   // 1:防雷告警 0:正常

	quint16 m_emergStopCountSec;
	quint16 m_emergStopSignalSec; // 1:急停信号 0:无 (只记录日志不通知BA）
	quint16 m_emergStopAlarmSec;  // 1:急停  0:正常

	quint16 m_bmsLocalCount;
	quint16 m_bmsLocalSignal;
	quint16 m_bmsLocalState;     // 1:BMS就地 0:BMS远方

	quint16 m_bmsRemoteCount;
	quint16 m_bmsRemoteSignal;
	quint16 m_bmsRemoteState;   // 1:BMS远方 0:S就地

	quint16 m_combusGasCount;
	quint16 m_combusGasSignal;// 1:可燃气体信号 0:无 (只记录日志不通知BA）
	quint16 m_combusGasAlarm; // 1:可燃气体告警 0:正常
	quint16 m_combusGasDetectAlarm; // added 2022-5-25

	quint16 m_isolSwitchClosedCount; // 隔离开关闭合记数 // added 2022-03-30
	quint16 m_isolSwitchOpendCount;  // 隔离开关断开记数 

	quint16 m_mainSwitchCount;   // added 2024-1-23
	quint16 m_mainSwitchClosed;  // 主断路器状态 1:闭合 0:断开

	quint16 m_acFaultCount;
	quint16 m_acFault;           // 1:空调故障     0:无 

	quint16 m_lqFaultCount;
	quint16 m_lqFault;          // 1:液冷机故障      0:无 
	
	quint16 m_hlkgState[MAX_HLKG_COUNT];   // 汇流开关反馈，一般只有1个
	quint16 m_glkgState[MAX_GLKG_COUNT];   // 隔离开关反馈

	quint16 m_xfTkMotionCount;// 消防脱扣执行次数
	quint16 m_xfTkMotionLogCount;

	quint16 m_xfSprayTkMotionCount;// 消防喷洒脱扣执行次数 // added 2022-12-10
	quint16 m_xfSprayTkMotionLogCount;
	
	quint16 m_volDownProtTkCount;
	quint16 m_volDownProtTkLogCount;

	quint16 m_reservedCount[10];
	quint16 m_reservedAlarm[10];
private:
	quint16   m_stackState;    // 当前堆状态
	QDateTime m_statChangeTime;// 堆状态改变时间
	
	quint16   m_drvFzCount;   // 已发送分闸/脱扣命令记数
	quint16   m_drvFzSuccedCount;// added 2022-1-11
	QDateTime m_drvFzTime;    // 最近一次驱动分闸时间

	quint16   m_drvHzCount;   // 已发送合闸命令记数
	quint16   m_drvHzSuccedCount;
	QDateTime m_drvHzTime;    // 最近一次驱动合闸时间
	quint16   m_oldConnState; //  并网连接状态 

	quint16   m_masterFzCount;// 主动分闸记数
	quint16   m_masterHzCount;// 主动合闸记数

	quint16   m_saveEmsCtrlHzRdCount;// 保存EMS控制汇流开关记录后计时
	quint16   m_saveEmsCtrlFzRdCount;
	quint16   m_saveRejectEmsHzRdCount;

	quint16   m_detectYcCount;
	quint16   m_detectOkCount;

	quint16 m_usOldHasAlarm;
	quint16 m_usOldHasProt;

	quint16 m_usOldChgJcqState;
	quint16 m_usOldDchgJcqState;

	quint16 m_usConnFailed;  // 是否有通迅故障 0:无 1:有, 99:没有初始化

	//int m_iLogCount;
	quint16 m_signalLogCount; // 共用, 用于不过多产生日志

	quint16 m_sendHzCount; // added 2023-11-19
	quint16 m_hzSucced;
};

#endif // DRYCTRL_THREAD_H
